/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* dfs(ListNode* head)
    {
        if (head->next == nullptr) return head;
        dfs(head->next)->next = head;
        head->next = nullptr;
        return head;
    }
    ListNode* reverseList(ListNode* head) {
        ListNode* tmp = head;
        if (tmp == nullptr) return head;
        while (tmp->next)
        {
            tmp = tmp->next;
        }
        dfs(head);
        return tmp;




    }
};